package com.rsxy.service.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rsxy.service.pojo.RsNeeds;
import com.rsxy.service.pojo.RsProduct;
import com.rsxy.service.service.RsCommentService;
import com.rsxy.service.service.RsNeedsService;
import com.rsxy.service.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/need")
@Slf4j
@Api(tags= "需求相关接口")
public class NeedsController {
    @Autowired
    private RsNeedsService rsNeedsService;
    @Autowired
    private RsCommentService rsCommentService;
    @ApiOperation(value = "发布需求",notes = "测试数据")
    @RequestMapping(method = RequestMethod.POST,value = "/addNeed")
    public String addNeed(RsNeeds rsNeeds){
        int insert = rsNeedsService.getBaseMapper().insert(rsNeeds);
        if (insert>0){
            return ResultUtil.success(200,"发布成功",null);
        }else {
            return ResultUtil.success(200,"发布失败",null);
        }
    }
    @ApiOperation(value = "获取需求分页列表",notes = "测试数据")
    @RequestMapping(method = RequestMethod.GET,value = "/getPageList")
    public IPage<RsNeeds> getPageList(Integer page,Integer limit){
        Page<RsNeeds> page2=new Page<>(page,limit);
        IPage<RsNeeds> page1 = rsNeedsService.getBaseMapper().selectPage(page2, null);
        return page1;
    }
    @ApiOperation(value = "获取需求列表",notes = "测试数据")
    @RequestMapping(method = RequestMethod.GET,value = "/getNeedList")
    public String getNeedList(){
        List<RsNeeds> needList = rsNeedsService.getNeedList();
        return ResultUtil.success(200,"获取成功",needList);
    }

    @RequestMapping(method = RequestMethod.DELETE,value = "/{productId}")
    public String deleteById(@PathVariable("productId") String productId){
        QueryWrapper<RsNeeds> rsProductQueryWrapper=new QueryWrapper<>();
        rsProductQueryWrapper.eq("need_id",productId);
        int delete = rsNeedsService.getBaseMapper().delete(rsProductQueryWrapper);
        if (delete>0){
            Map<String, Object> map = new HashMap<>();
            map.put("comment_from",productId);
            int i = rsCommentService.getBaseMapper().deleteByMap(map);
        }
        return ResultUtil.success(200,"删除成功",null);
    }

    @ApiOperation(value = "获取个人所有订单",notes = "测试数据")
    @RequestMapping(method = RequestMethod.GET,value="/{userId}/v1")
    public IPage<RsNeeds> getPageInfoList(@PathVariable("userId") String userId,Integer page, Integer limit){
        QueryWrapper<RsNeeds> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<RsNeeds> page2=new Page<>(page,limit);
        IPage<RsNeeds> page1 = rsNeedsService.getBaseMapper().selectPage(page2, wrapper);
        return page1;
    }
}
